package com.zxq.kingofbots.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.kingofbots.mapper.UserMapper;
import com.zxq.kingofbots.pojo.User;
import com.zxq.kingofbots.service.user.account.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Map<String, String> register(String username, String password, String confirmedPassword) {
        Map<String,String> map=new HashMap<>();
        if(username==null){
            map.put("message","用户名不能为空");
            return map;
        }

        if(password==null||confirmedPassword==null){
            map.put("message","密码和确认密码不能为空");
            return map;
        }

        username=username.trim();
        if(username==null){
            map.put("message","用户名不能为空");
            return map;
        }

        if(username.length()>100){
            map.put("message","用户名长度不能大于100");
            return map;
        }

        if(password.length()==0||confirmedPassword.length()==0){
            map.put("message","密码不能为空");
            return map;
        }

        if(!password.equals(confirmedPassword)){
            map.put("message","两次密码不一致");
            return map;
        }

        if(password.length()>100){
            map.put("message","密码不能大于100");
            return map;
        }

        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User user = userMapper.selectOne(queryWrapper);
        if(user!=null){
            map.put("message","用户名已存在");
            return map;
        }

        String encode = passwordEncoder.encode(password);
        String photo="https://cdn.acwing.com/media/user/profile/photo/404551_lg_ad2583d5b7.JPG";
        User insertUser = new User(null, username, encode, photo,null);
        userMapper.insert(insertUser);
        map.put("message","success");
        return map;
    }
}
